package com.htc.android.footprints.provider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.htc.android.footprints.vo.Category;
import com.htc.android.footprints.vo.Footprint;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class FootprintManager
{
  private static final String[] CATEGORY_PROJECTION;
  private static final String[] FOOTPRINT_PROJECTION;
  private static final String TAG = "FootprintManager";
  private static int idxAccuracy;
  private static int idxAddr;
  private static int idxAudioLength;
  private static int idxAudioName;
  private static int idxAudioPath;
  private static int idxCategory;
  private static int idxComment;
  private static int idxCreateTime;
  private static int idxDrawableId;
  private static int idxId;
  private static int idxLatitude;
  private static int idxLongitude;
  private static int idxName;
  private static int idxPicName;
  private static int idxPicPath;
  private static int idxRate;
  private static int idxShortDate;
  private static int idxSite;
  private static int idxTel;
  private static int idxVersion;
  private static FootprintManager mInstance = null;
  private ContentResolver mDB = null;

  static
  {
    String[] arrayOfString1 = new String[20];
    arrayOfString1[0] = "_id";
    arrayOfString1[1] = "name";
    arrayOfString1[2] = "tel";
    arrayOfString1[3] = "addr";
    arrayOfString1[4] = "comment";
    arrayOfString1[5] = "category";
    arrayOfString1[6] = "create_time";
    arrayOfString1[7] = "latitude";
    arrayOfString1[8] = "longitude";
    arrayOfString1[9] = "site";
    arrayOfString1[10] = "rate";
    arrayOfString1[11] = "drawable_id";
    arrayOfString1[12] = "version";
    arrayOfString1[13] = "audio_path";
    arrayOfString1[14] = "pic_path";
    arrayOfString1[15] = "accuracy";
    arrayOfString1[16] = "pic_name";
    arrayOfString1[17] = "audio_name";
    arrayOfString1[18] = "audio_length";
    arrayOfString1[19] = "short_date";
    FOOTPRINT_PROJECTION = arrayOfString1;
    String[] arrayOfString2 = new String[3];
    arrayOfString2[0] = "keyword";
    arrayOfString2[1] = "name";
    arrayOfString2[2] = "icon_id";
    CATEGORY_PROJECTION = arrayOfString2;
    idxId = -1;
    idxName = -1;
    idxTel = -1;
    idxAddr = -1;
    idxComment = -1;
    idxCategory = -1;
    idxCreateTime = -1;
    idxLatitude = -1;
    idxLongitude = -1;
    idxSite = -1;
    idxRate = -1;
    idxDrawableId = -1;
    idxVersion = -1;
    idxAudioPath = -1;
    idxPicPath = -1;
    idxAccuracy = -1;
    idxPicName = -1;
    idxAudioName = -1;
    idxAudioLength = -1;
    idxShortDate = -1;
  }

  private FootprintManager(Context paramContext)
  {
    this.mDB = paramContext.getContentResolver();
  }

  public static boolean checkContentValues(ContentValues paramContentValues)
  {
    int i = 1;
    String str1 = paramContentValues.getAsString("name");
    if ((str1 == null) || (str1.length() == 0))
      i = 0;
    if (paramContentValues.getAsLong("create_time").longValue() == 0L)
      i = 0;
    String str2 = paramContentValues.getAsString("pic_path");
    if ((str2 == null) || (str2.length() == 0))
      i = 0;
    return i;
  }

  public static boolean checkContentValues(Footprint paramFootprint)
  {
    int i = 1;
    if ((paramFootprint.mName == null) || (paramFootprint.mName.length() == 0))
      i = 0;
    if (paramFootprint.mCreateTime == 0L)
      i = 0;
    if ((paramFootprint.mPicPath == null) || (paramFootprint.mPicPath.length() == 0))
      i = 0;
    return i;
  }

  private void fillDataFromCursor(Category paramCategory, Cursor paramCursor)
  {
    paramCategory.mKeyword = paramCursor.getString(paramCursor.getColumnIndexOrThrow("keyword"));
    paramCategory.mName = paramCursor.getString(paramCursor.getColumnIndexOrThrow("name"));
    paramCategory.mIconID = paramCursor.getInt(paramCursor.getColumnIndexOrThrow("icon_id"));
  }

  public static void fillDataFromCursor(Footprint paramFootprint, Cursor paramCursor)
  {
    if (idxId == -1)
    {
      idxId = paramCursor.getColumnIndexOrThrow("_id");
      idxName = paramCursor.getColumnIndexOrThrow("name");
      idxTel = paramCursor.getColumnIndexOrThrow("tel");
      idxAddr = paramCursor.getColumnIndexOrThrow("addr");
      idxComment = paramCursor.getColumnIndexOrThrow("comment");
      idxCategory = paramCursor.getColumnIndexOrThrow("category");
      idxCreateTime = paramCursor.getColumnIndexOrThrow("create_time");
      idxLatitude = paramCursor.getColumnIndexOrThrow("latitude");
      idxLongitude = paramCursor.getColumnIndexOrThrow("longitude");
      idxSite = paramCursor.getColumnIndexOrThrow("site");
      idxRate = paramCursor.getColumnIndexOrThrow("rate");
      idxDrawableId = paramCursor.getColumnIndexOrThrow("drawable_id");
      idxVersion = paramCursor.getColumnIndexOrThrow("version");
      idxAudioPath = paramCursor.getColumnIndexOrThrow("audio_path");
      idxPicPath = paramCursor.getColumnIndexOrThrow("pic_path");
      idxAccuracy = paramCursor.getColumnIndexOrThrow("accuracy");
      idxPicName = paramCursor.getColumnIndexOrThrow("pic_name");
      idxAudioName = paramCursor.getColumnIndexOrThrow("audio_name");
      idxAudioLength = paramCursor.getColumnIndexOrThrow("audio_length");
      idxShortDate = paramCursor.getColumnIndexOrThrow("short_date");
    }
    paramFootprint.setID(paramCursor.getInt(idxId));
    paramFootprint.mName = paramCursor.getString(idxName);
    paramFootprint.mTel = paramCursor.getString(idxTel);
    paramFootprint.mAddr = paramCursor.getString(idxAddr);
    paramFootprint.mComment = paramCursor.getString(idxComment);
    paramFootprint.mCategory = paramCursor.getString(idxCategory);
    paramFootprint.mCreateTime = paramCursor.getLong(idxCreateTime);
    paramFootprint.mLatitude = paramCursor.getInt(idxLatitude);
    paramFootprint.mLongitude = paramCursor.getInt(idxLongitude);
    paramFootprint.mSite = paramCursor.getString(idxSite);
    paramFootprint.mRate = paramCursor.getInt(idxRate);
    paramFootprint.mDrewableID = paramCursor.getLong(idxDrawableId);
    paramFootprint.mVersion = paramCursor.getInt(idxVersion);
    paramFootprint.mAudioPath = paramCursor.getString(idxAudioPath);
    paramFootprint.mPicPath = paramCursor.getString(idxPicPath);
    paramFootprint.mAccuracy = paramCursor.getInt(idxAccuracy);
    paramFootprint.mPicFileName = paramCursor.getString(idxPicName);
    paramFootprint.mAudioFileName = paramCursor.getString(idxAudioName);
    paramFootprint.mVoiceSampleLength = paramCursor.getFloat(idxAudioLength);
    paramFootprint.mShortDate = paramCursor.getString(idxShortDate);
  }

  private void fillVauleFromCategory(ContentValues paramContentValues, Category paramCategory)
  {
    paramContentValues.put("keyword", paramCategory.mKeyword);
    paramContentValues.put("name", paramCategory.mName);
    paramContentValues.put("icon_id", Integer.valueOf(paramCategory.mIconID));
  }

  public static void fillVauleFromFootprint(ContentValues paramContentValues, Footprint paramFootprint)
  {
    if (paramFootprint.getID() > -1)
      paramContentValues.put("_id", Integer.valueOf(paramFootprint.getID()));
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM/dd/yy");
    paramContentValues.put("name", paramFootprint.mName);
    paramContentValues.put("tel", paramFootprint.mTel);
    paramContentValues.put("addr", paramFootprint.mAddr);
    paramContentValues.put("comment", paramFootprint.mComment);
    paramContentValues.put("category", paramFootprint.mCategory);
    paramContentValues.put("create_time", Long.valueOf(paramFootprint.mCreateTime));
    paramContentValues.put("latitude", Integer.valueOf(paramFootprint.mLatitude));
    paramContentValues.put("longitude", Integer.valueOf(paramFootprint.mLongitude));
    paramContentValues.put("site", paramFootprint.mSite);
    paramContentValues.put("rate", Integer.valueOf(paramFootprint.mRate));
    paramContentValues.put("drawable_id", Long.valueOf(paramFootprint.mDrewableID));
    paramContentValues.put("version", Integer.valueOf(paramFootprint.mVersion));
    paramContentValues.put("audio_path", paramFootprint.mAudioPath);
    paramContentValues.put("pic_path", paramFootprint.mPicPath);
    paramContentValues.put("accuracy", Integer.valueOf(paramFootprint.mAccuracy));
    paramContentValues.put("pic_name", paramFootprint.mPicFileName);
    paramContentValues.put("audio_name", paramFootprint.mAudioFileName);
    paramContentValues.put("audio_length", Float.valueOf(paramFootprint.mVoiceSampleLength));
    paramContentValues.put("short_date", localSimpleDateFormat.format(new Date(paramFootprint.mCreateTime)));
  }

  public static FootprintManager getInstance(Context paramContext)
  {
    if (mInstance == null)
      mInstance = new FootprintManager(paramContext.getApplicationContext());
    return mInstance;
  }

  private String quotedStr(String paramString)
  {
    return "'" + paramString + "'";
  }

  public Uri addCategory(Category paramCategory)
  {
    ContentValues localContentValues = new ContentValues();
    fillVauleFromCategory(localContentValues, paramCategory);
    return this.mDB.insert(CategoryProvider.CategoryColumns.CONTENT_URI, localContentValues);
  }

  public void addCategory(Category[] paramArrayOfCategory)
  {
    int i = paramArrayOfCategory.length;
    for (int j = 0; j < i; j++)
      addCategory(paramArrayOfCategory[j]);
  }

  /** @deprecated */
  public int addFootprint(Footprint paramFootprint)
  {
    monitorenter;
    try
    {
      ContentValues localContentValues = new ContentValues();
      fillVauleFromFootprint(localContentValues, paramFootprint);
      Uri localUri = null;
      if (checkContentValues(localContentValues))
        localUri = this.mDB.insert(FootprintsProvider.FootprintColumns.CONTENT_URI, localContentValues);
      int i = -1;
      if (localUri != null)
      {
        int j = Integer.parseInt(localUri.getLastPathSegment());
        i = j;
      }
      monitorexit;
      return i;
    }
    finally
    {
      localObject = finally;
      monitorexit;
    }
    throw localObject;
  }

  public void addFootprint(Footprint[] paramArrayOfFootprint)
  {
    int i = paramArrayOfFootprint.length;
    for (int j = 0; j < i; j++)
      addFootprint(paramArrayOfFootprint[j]);
  }

  public void addNonDuplicatedFootprint(Footprint[] paramArrayOfFootprint)
  {
    int i = paramArrayOfFootprint.length;
    for (int j = 0; j < i; j++)
    {
      Footprint localFootprint = paramArrayOfFootprint[j];
      if ((!checkContentValues(localFootprint)) || (checkExistedFootprintByCreateTime(localFootprint.mCreateTime)))
        continue;
      addFootprint(localFootprint);
    }
  }

  public boolean checkExistedFootprintByCreateTime(long paramLong)
  {
    int i = 0;
    String str = "create_time = '" + paramLong + "'";
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, null);
    if (localCursor.moveToFirst())
      if (localCursor.getCount() <= 0)
        break label80;
    label80: for (i = 1; ; i = 0)
    {
      localCursor.close();
      return i;
    }
  }

  public Category findCategory(String paramString)
  {
    String str = "keyword = '" + paramString + "'";
    Cursor localCursor = this.mDB.query(CategoryProvider.CategoryColumns.CONTENT_URI, CATEGORY_PROJECTION, str, null, null);
    Category localCategory = null;
    if (localCursor.moveToNext())
    {
      localCategory = new Category();
      fillDataFromCursor(localCategory, localCursor);
    }
    localCursor.close();
    return localCategory;
  }

  public ArrayList<Footprint> findFootprintByCategory(String paramString1, String paramString2)
  {
    ArrayList localArrayList = new ArrayList();
    String str = null;
    if ((paramString1 != null) && (!paramString1.equals("")) && (!paramString1.equals(Category.getAllCategory().mKeyword)))
      str = "category  = " + quotedStr(paramString1);
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, paramString2);
    while (localCursor.moveToNext())
    {
      Footprint localFootprint = new Footprint();
      fillDataFromCursor(localFootprint, localCursor);
      localArrayList.add(localFootprint);
    }
    localCursor.close();
    return localArrayList;
  }

  public ArrayList<Footprint> findFootprintByMatchString(String paramString1, String paramString2, String paramString3)
  {
    new Footprint();
    String str = "name like '%" + paramString2 + "%'";
    if ((paramString1 != null) && (!paramString1.equals("")))
      str = str + " AND category  = " + quotedStr(paramString1);
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, paramString3);
    localCursor.deactivate();
    ArrayList localArrayList = new ArrayList(localCursor.getCount());
    while (localCursor.moveToNext())
    {
      Footprint localFootprint = new Footprint();
      fillDataFromCursor(localFootprint, localCursor);
      localArrayList.add(localFootprint);
    }
    localCursor.close();
    return localArrayList;
  }

  public Cursor findFootprintCursorByCategory(String paramString1, String paramString2)
  {
    String str = null;
    if ((paramString1 != null) && (!paramString1.equals("")) && (!paramString1.equals(Category.getAllCategory().mKeyword)))
      str = "category  = " + quotedStr(paramString1);
    return this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, paramString2);
  }

  public Cursor findFootprintCursorByCategoryAndKeyword(String paramString1, String paramString2)
  {
    String str = "accuracy > 0 ";
    if ((paramString1 != null) && (!paramString1.equals("")) && (!paramString1.equals(Category.getAllCategory().mKeyword)))
      str = str + "AND category  = " + quotedStr(paramString1);
    if ((paramString2 != null) && (!paramString2.equals("")))
      str = str + "AND name  like " + quotedStr(new StringBuilder().append("%").append(paramString2).toString());
    return this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, null);
  }

  public ArrayList<Footprint> findFootprintWithoutMatchString(String paramString1, String paramString2, String paramString3)
  {
    new Footprint();
    String str = "name not in (select name from footprint where name like '%" + paramString2 + "%')";
    if ((paramString1 != null) && (!paramString1.equals("")))
      str = str + " AND category  = " + quotedStr(paramString1);
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, paramString3);
    ArrayList localArrayList = new ArrayList(localCursor.getCount());
    localCursor.moveToFirst();
    while (localCursor.moveToNext())
    {
      Footprint localFootprint = new Footprint();
      fillDataFromCursor(localFootprint, localCursor);
      localArrayList.add(localFootprint);
    }
    localCursor.close();
    return localArrayList;
  }

  public ArrayList<Category> getAllCategory()
  {
    Cursor localCursor = this.mDB.query(CategoryProvider.CategoryColumns.CONTENT_URI, CATEGORY_PROJECTION, null, null, null);
    localCursor.deactivate();
    ArrayList localArrayList = new ArrayList(localCursor.getCount());
    while (localCursor.moveToNext())
    {
      Category localCategory = new Category();
      fillDataFromCursor(localCategory, localCursor);
      localArrayList.add(localCategory);
    }
    localCursor.close();
    return localArrayList;
  }

  public Cursor getCursorSortBy(String paramString1, String paramString2, String paramString3)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    if ((paramString1 != null) && (!paramString1.equals("")))
      localStringBuilder.append("category").append(" = ").append(quotedStr(paramString1));
    if ((paramString2 != null) && (!paramString2.equals("")))
    {
      if (localStringBuilder.length() <= 0)
        break label220;
      localStringBuilder.append(" AND (");
    }
    while (true)
    {
      localStringBuilder.append("name").append(" like '% ").append(paramString2).append("%' ");
      localStringBuilder.append(" OR ");
      localStringBuilder.append("name").append(" like '").append(paramString2).append("%' ");
      localStringBuilder.append(" OR ");
      localStringBuilder.append("addr").append(" like '% ").append(paramString2).append("%' ");
      localStringBuilder.append(" OR ");
      localStringBuilder.append("addr").append(" like '").append(paramString2).append("%') ");
      return this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, localStringBuilder.toString(), null, paramString3);
      label220: localStringBuilder.append(" (");
    }
  }

  public long getExistFootprintID(Footprint paramFootprint)
  {
    long l = -1L;
    String str = null;
    if (paramFootprint.mCreateTime > 0L)
      str = "create_time = '" + paramFootprint.mCreateTime + "'";
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, null);
    if (localCursor.moveToNext())
      l = localCursor.getLong(localCursor.getColumnIndexOrThrow("_id"));
    localCursor.close();
    return l;
  }

  public ArrayList<Footprint> getFootprintAll(String paramString)
  {
    return findFootprintByCategory(null, paramString);
  }

  public Footprint getFootprintById(int paramInt)
  {
    Footprint localFootprint = new Footprint();
    String str = "_id = '" + String.valueOf(paramInt) + "'";
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, null);
    while (localCursor.moveToNext())
      fillDataFromCursor(localFootprint, localCursor);
    localCursor.close();
    return localFootprint;
  }

  public Footprint getFootprintByName(String paramString)
  {
    Footprint localFootprint = new Footprint();
    String str = "name = '" + paramString + "'";
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, "create_time desc");
    if (localCursor.moveToNext())
      fillDataFromCursor(localFootprint, localCursor);
    localCursor.close();
    return localFootprint;
  }

  public ArrayList<Footprint> getFootprintByRange(String paramString, int paramInt1, int paramInt2)
  {
    String str2;
    if (paramInt1 >= 0)
    {
      StringBuilder localStringBuilder1 = new StringBuilder();
      if (paramString != null);
      for (String str1 = paramString; ; str1 = "_id")
      {
        str2 = str1 + " limit " + paramInt2 + " offset " + paramInt1;
        return findFootprintByCategory(null, str2);
      }
    }
    StringBuilder localStringBuilder2 = new StringBuilder();
    if (paramString != null);
    for (String str3 = paramString; ; str3 = "_id")
    {
      str2 = str3 + " limit " + paramInt2 + "  offset (select count(_id) from footprint) " + paramInt1;
      break;
    }
  }

  public boolean hasFootprints()
  {
    if (!getFootprintByRange(null, 0, 1).isEmpty());
    for (int i = 1; ; i = 0)
      return i;
  }

  public boolean isExistFootprint(Footprint paramFootprint)
  {
    int i = 0;
    String str = null;
    if (paramFootprint.mCreateTime > 0L)
      str = "create_time = '" + paramFootprint.mCreateTime + "'";
    Cursor localCursor = this.mDB.query(FootprintsProvider.FootprintColumns.CONTENT_URI, FOOTPRINT_PROJECTION, str, null, null);
    if (localCursor.getCount() > 0)
      i = 1;
    localCursor.close();
    return i;
  }

  public void modifyCategory(Category paramCategory)
  {
    ContentValues localContentValues = new ContentValues();
    fillVauleFromCategory(localContentValues, paramCategory);
    String str = "keyword = '" + paramCategory.mKeyword + "'";
    this.mDB.update(CategoryProvider.CategoryColumns.CONTENT_URI, localContentValues, str, null);
  }

  public void modifyFootprint(Footprint paramFootprint)
  {
    ContentValues localContentValues = new ContentValues();
    fillVauleFromFootprint(localContentValues, paramFootprint);
    String str = "_id = '" + String.valueOf(paramFootprint.getID()) + "'";
    this.mDB.update(FootprintsProvider.FootprintColumns.CONTENT_URI, localContentValues, str, null);
  }

  public void modifyFootprintbyImport(Footprint paramFootprint)
  {
    ContentValues localContentValues = new ContentValues();
    fillVauleFromFootprint(localContentValues, paramFootprint);
    String str = "create_time = '" + paramFootprint.mCreateTime + "'";
    this.mDB.update(FootprintsProvider.FootprintColumns.CONTENT_URI, localContentValues, str, null);
  }

  public void removeAllFootprint()
  {
    this.mDB.delete(FootprintsProvider.FootprintColumns.CONTENT_URI, null, null);
  }

  public void removeCategory(Category paramCategory)
  {
    String str = "keyword = '" + paramCategory.mKeyword + "'";
    this.mDB.delete(CategoryProvider.CategoryColumns.CONTENT_URI, str, null);
  }

  public void removeFootprint(int paramInt)
  {
    removeFootprint(String.valueOf(paramInt));
  }

  public void removeFootprint(Footprint paramFootprint)
  {
    removeFootprint(paramFootprint.getID());
  }

  public void removeFootprint(String paramString)
  {
    String str = "_id = '" + paramString + "'";
    this.mDB.delete(FootprintsProvider.FootprintColumns.CONTENT_URI, str, null);
  }

  public void removeFootprint(Footprint[] paramArrayOfFootprint)
  {
    StringBuilder localStringBuilder = new StringBuilder("_id");
    localStringBuilder.append(" in (");
    int i = 1;
    int j = paramArrayOfFootprint.length;
    for (int k = 0; k < j; k++)
    {
      Footprint localFootprint = paramArrayOfFootprint[k];
      if (i == 0)
        localStringBuilder.append(",");
      i = 0;
      localStringBuilder.append(localFootprint.getID());
    }
    localStringBuilder.append(")");
    this.mDB.delete(FootprintsProvider.FootprintColumns.CONTENT_URI, localStringBuilder.toString(), null);
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.provider.FootprintManager
 * JD-Core Version:    0.6.0
 */